from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from cart import models,cartser
# Create your views here.
from utils.authentication import JWTAuthenticationss
class AddCart(APIView):
    authentication_classes = [JWTAuthenticationss]
    def post(self,request):
        user=request.data.get("user")
        course=request.data.get("course")
        #当前用户的当前课程是否存在购物车中
        info=models.CartModel.objects.filter(user=user,course=course).first()
        if info:
            return Response({"code": 200, "msg": "购物车已存在"})
        else:
            #添加
            ser=cartser.CartSerializer(data=request.data)
            if ser.is_valid():
                ser.save()
                return Response({"code": 200, "msg": "添加成功"})
            else:
                return Response({"code": 204, "msg": ser.errors})
class CartList(APIView):
    authentication_classes = [JWTAuthenticationss]
    def get(self,request):
        #获取到用户id
        user=request.query_params.get("user")
        print(user)
        datalist=models.CartModel.objects.filter(user=user)#queryset
        ser=cartser.CartSerializer(datalist,many=True)
        # cartdata=[]
        # total=0#总价
        # selectnum=0
        # for i in datalist:
        #     course={}
        #     course["id"]=i.id#购物车主键
        #     course["courseid"]=i.course.id#课程主键
        #     course["name"]=i.course.name#课程名
        #     course["price"]=i.course.price#课程价格
        #     course["img"]=str(i.course.image)#课程图片
        #     course["selected"]=i.selcted#是否选中
        #     course["addtime"]=i.created_time#添加时间
        #     cartdata.append(course)
        #     if i.selcted:
        #         selectnum+=1
        #         total+=i.course.price
        #return Response({"msg":"ok","cartlist":cartdata,"total":total,"selectnum":selectnum})
        total=0
        selectnum=0
        for i in ser.data:
            if i["selcted"]:
                selectnum+=1
                total+=i["courselist"].get("price")
        return Response({"msg":"ok","cartlist":ser.data,"total":total,"selectnum":selectnum})

#修改状态
class CartUpdate(APIView):
    authentication_classes = [JWTAuthenticationss]
    def put(self,request):
        id=request.data.get("id")#购物车id
        cartdata=models.CartModel.objects.get(id=id)
        # if cartdata.selcted:
        #     cartdata.selcted=False
        # else:
        #     cartdata.selcted=True
        cartdata.selcted=not cartdata.selcted
        cartdata.save()
        return Response({"msg":"修改成功"})
#单个删除
class CartDelete(APIView):
    authentication_classes = [JWTAuthenticationss]
    def delete(self,request):
        id=request.query_params.get("id")#购物车id
        print(id)
        models.CartModel.objects.get(id=id).delete()
        return Response({"msg":"删除成功"})
#批量删除
class CartMany(APIView):
    authentication_classes = [JWTAuthenticationss]
    def delete(self,request):
        id=request.query_params.get("user")#用户ID
        models.CartModel.objects.get(user=id,selcted=True).delete()
        return Response({"msg":"删除成功"})

#批量删除2
class CartMuch(APIView):
    authentication_classes = [JWTAuthenticationss]
    def delete(self,request):
        #ids  1,2,3,4,
        ids=request.query_params.get("ids")#购物车id 用，拼接
        idlist=ids.split(",")
        data=models.CartModel.objects.filter(id__in=idlist).delete()
        #models.CartModel.objects.get(user=id,selcted=True).delete()
        return Response({"msg":"删除成功"})

#全选
class CartAll(APIView):
    authentication_classes = [JWTAuthenticationss]
    def put(self,request):
        user=request.data.get("user")#用户id
        status=request.data.get("status")#全选状态
        models.CartModel.objects.filter(user=user).update(selcted=status)
        # data=models.CartModel.objects.filter(user=user)
        # for i in data:
        #     i.selcted=status
        #     i.save()
        if status:
            return Response({"msg": "全选成功"})
        else:
            return Response({"msg": "取消全选成功"})

#选中的商品
class CartSel(APIView):
    authentication_classes = [JWTAuthenticationss]
    def get(self,request):
        user=request.query_params.get("user")
        cartdata=models.CartModel.objects.filter(user=user,selcted=True)
        ser=cartser.CartSerializer(cartdata,many=True)
        total=0
        for i in ser.data:
            total+=i["courselist"]["price"]
        # cartlist=[]
        # for i in cartdata:
        #     one={}
        #     one["id"]=i.id
        #     one["selcted"]=i.selcted
        #     one["name"]=i.course.name
        #     one["price"]=i.course.price
        #     cartlist.append(one)
        return Response({"msg":"ok","data":ser.data,"total":total})
